<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button v-allow="'import_excel'" type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
          <el-button v-allow="'export_excel'" type="danger" size="small" @click="hExportExcel">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <!-- 定义需要根据字段排序的字段名称 -->
        <el-table border :data="list" :default-sort="{ prop: 'workNumber' }">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" prop="staffPhoto" >
            <template slot-scope="scope">
              <ImageHolder :src="scope.row.staffPhoto"/>
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template slot-scope="scope">
              {{ format(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态"  /> -->
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push('/employees/detail?id='+scope.row.id)" >查看</el-button>
              <el-button type="text" size="small" @click="hAssignRole(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :total="total"
            :current-page="page"
            :page-size="size"
            :page-sizes="[2, 5, 10, 20]"
            layout="total,sizes,prev, pager, next"
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          />
        </el-row>
      </el-card>
    </div>
    <el-dialog
      v-if="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="添加"
      :visible.sync="showDialog"
    >
    <EmpDialog
    @success= "hSuccess"
    @close="showDialog=false"
    />
    </el-dialog>
    <!-- 分配角色 -->
    <el-dialog :visible.sync="showDialogRole">
  <AssignRole
    v-if="showDialogRole"
    :curId = 'curId'
    @success ='hSuccessRole'
  />
</el-dialog>
  </div>
</template>
<script>
const typeList = {
  1: "正式",
  2: "非正式",
  3: "AAA",
  4: "BBB",
  5: "CCC",
  6: "DDD",
};
import { getEmployeeList, delEmployee} from "@/api/employees";
import EmpDialog from './EmpDialog.vue'
import {TYPE_MAP}from '@/constant/index'
import AssignRole from '@/views/employees/assignRole.vue'
export default {
  components: { EmpDialog , AssignRole },
  data() {
    return {
      curId:'',
      list: [],
      //分页
      page: 1, // 当前页码
      size: 5, // 每页几条
      total: 0, // 总共数据条数
       showDialog: false,
       showDialogRole: false  
    };
  },
  methods: {
    hSuccessRole(){
      this.showDialogRole = false
      this.$message.success('保存成功')
    }
    //分配角色
    ,
    hAssignRole(id){
      this.showDialogRole=true
      this.curId = id
    }
    ,
    format(type) {
      return TYPE_MAP[type];
    },

    //分页功能嗯
    hSizeChange(curPagesize) {
      console.log(curPagesize);
      // 1. 更新参数
      this.size = curPagesize;
      this.page = 1;
      // 2. 重发请求
      this.getList();
    },
    // 自动接收当前的页码
    hCurrentChange(curPage) {
      console.log(curPage);
      // 1. 更新页码
      this.page = curPage;
      // 2. 重发请求
      this.getList();
    },

    async getList() {
      const res = await getEmployeeList({ page: this.page, size: this.size });
      console.log(res);
      this.list = res.data.rows;
      this.total = res.data.total;
    },

    //删除
    async doDel(id) {
      const res = await delEmployee(id);
      if (this.list.length === 1) {
        if (this.page > 1) {
          this.page--;
        }
      }
      this.getList();
    },
    hDel(id) {
      this.$confirm("你确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.doDel(id);
        })
        .catch(() => {});
    },
    hSuccess(){
      this.showDialog = false
      this.getList()
    }
    ,
   formatData(rows) {
      // 表头
      // const header = Object.keys(rows[0])
      const enHeader = Object.keys(rows[0])
      const mapObj = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      const header = enHeader.map(en=>mapObj[en])
      // 内容
      const data = rows.map(obj => {
        obj.formOfEmployment = TYPE_MAP[obj.formOfEmployment]
        return Object.values(obj) })

      return { header, data }
    },
    async hExportExcel() {
      // 1. 发请求拿数据
      const res = await getEmployeeList({ page: this.page, size: this.size })
      // 2. 对数据进行格式转换
      const { header, data } = this.formatData(res.data.rows)
      // 3. 导出
      console.log('员工信息', header, data)

      import('@/vendor/Export2Excel').then(excel => {
        // 准备数据
        // const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
        // const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
        // const list = this.list
        // const data = this.formatJson(filterVal, list)

        // 生成excel
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: '员工信息', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },

 
},
 created() {
    this.getList();
  },
}
</script>
